package com.zhkj.txg.module.group.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.webkit.WebView
import androidx.core.content.ContextCompat
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.txg.R
import com.zhkj.txg.module.group.vm.GroupDetailViewModel
import com.zhkj.txg.module.product.ui.WatcherActivity
import com.zhkj.txg.utils.WebViewUtils
import com.zhkj.txg.utils.WebViewUtils.addImageClickListener
import kotlinx.android.synthetic.main.activity_group_rule.*

class GroupRuleActivity : ViewModelActivity<GroupDetailViewModel>() {

    companion object {
        private const val RULE = "RULE"
        fun start(context: Context, rule: String) {
            val intent = Intent(context, GroupRuleActivity::class.java)
                .putExtra(RULE, rule)
            context.startActivity(intent)
        }
    }

    private val webView: WebView by lazy {
        WebView(this)
    }

    override val layoutResId: Int = R.layout.activity_group_rule
    override fun setViewModel(): GroupDetailViewModel = createViewModel()
    override fun statusTitleBar(): View? = title_bar

    override fun initUI() {
    }

    override fun initData() {
        val rule = intent.getStringExtra(RULE)
        WebViewUtils.addWebView2Container(webView, container)
        WebViewUtils.initWebView(webView)
        webView.addImageClickListener {
            if (!it.isNullOrEmpty()) WatcherActivity.open(this, arrayListOf(it), 0)
        }
        WebViewUtils.loadFullScreenHtml(webView, rule ?: "")
        webView.setBackgroundColor(ContextCompat.getColor(this, R.color.colorLightGray))
    }

    override fun onDestroy() {
        WebViewUtils.removeWebView(webView, container)
        super.onDestroy()
    }
}